import Vue from 'vue'
import Vuex from 'vuex'
import {getAdminInfo} from '@/api/getData'
import {getCookie} from "../utils/cookie";
import {baseImgPath} from "../config/env";

Vue.use(Vuex)

const state = {
	adminInfo: {
		image: 'default.jpg'
	},
    token:getCookie('token')
}

const mutations = {
	saveAdminInfo(state, adminInfo){
		state.adminInfo = adminInfo;
		sessionStorage.setItem("adminInfo", adminInfo);
	},
    setToken(state, token){
	    state.token = token;
    },
}

const getters = {
}

const actions = {
	async getAdminData({commit}){
		try{
			const res = await getAdminInfo({token:getCookie("token")})
			if (res.data.code == 0) {
			    if(res.data.data.image){
                    res.data.data.image = baseImgPath + res.data.data.image;
                }else{
                    res.data.data.image = '';
                }

				commit('saveAdminInfo', res.data.data);
			}else{
                commit('saveAdminInfo', null);  // 后台session过期
			}
		}catch(err){
			// console.log(err.message)
		}
	}
}

export default new Vuex.Store({
	state,
	actions,
	mutations,
})
